<?php
namespace Admin\Controller;
use Frame\Libs\BaseController;
use Admin\Model\yewuModel;
use Admin\Model\BottomModel;
use Admin\Model\ImagesModel;

final class ReceptionController extends BaseController{
	public function yewu_index(){
		$this->denyAccess();
		$data = YewuModel::getInstance()->fetchAll();
		$this->smarty->assign("data",$data);
		$this->smarty->display("Reception/yewu_index.html");
	}
	public function yewu_edit(){
		$this->denyAccess();
		if(IS_GET){
			$id = $_GET["id"];
			$data = YewuModel::getInstance()->fetchOne("id=$id");
			$this->smarty->assign("data",$data);
			$this->smarty->display("Reception/yewu_edit.html");
		}else{
			$id = $_POST["id"];
			$title = $_POST["title"];
			$content = $_POST["content"];
			$res = YewuModel::getInstance()->edit("title='$title',content='$content'",$id);
			if($res){
				$this->write_log("修改前台业务部分");
				$this->jump("修改成功","?c=Reception&a=yewu_index");
			}else{
				$this->jump("修改失败","?c=Reception&a=yewu_index");
			}
		}
	}
	public function images_index(){
		$this->denyAccess();
		$data = ImagesModel::getInstance()->fetchAll();
		$this->smarty->assign("data",$data);
		$this->smarty->display("Reception/images_index.html");
	}
	public function images_edit(){
		$this->denyAccess();
		if(IS_GET){
			$id = $_GET["id"];
			$data = ImagesModel::getInstance()->fetchOne("id=".$id);
			$this->smarty->assign("data",$data);
			$this->smarty->display("Reception/Images_edit.html");
		}else{		
			$id = $_POST["id"];
			
			$uploaddir = dirname($_SERVER['SCRIPT_FILENAME'])."/Public/upload/qiantai_images/";	
			$uploadfile = $uploaddir.basename($_FILES['file']['name']);
			//url路径
			$url = "/qiye/Public/upload/qiantai_images/".basename($_FILES['file']['name']);
					
			$name = $_FILES["file"]['name'];
			$ext = substr(strrchr($name,'.'),1);
			
			$allow_type = ["jpg","png"];
			$allow = false;
			foreach($allow_type as $v){
				if($ext == $v){
					$allow = true;
				}
			}
			$allow || die("不允许上传该类型");
			if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){
				//删除原有图片
				$data = ImagesModel::getInstance()->fetchOne("id=$id");
				unlink(dirname(dirname($_SERVER['SCRIPT_FILENAME'])).$data["images"]);
				//修改数据库图片路径
				$res = ImagesModel::getInstance()->edit("images='$url'",$id);
				if($res){
					$this->write_log("修改前台图片部分");
					$this->jump("修改成功","?c=Reception&a=images_index");
				}else{
					$this->jump("修改失败","?c=Reception&a=images_index");
				}
			}
		}
	}
	public function bottom_index(){
		$this->denyAccess();
		$data = BottomModel::getInstance()->fetchAll();
		$this->smarty->assign("data",$data);
		$this->smarty->display("Reception/bottom_index.html");
	}
	public function bottom_edit(){
		$this->denyAccess();
		if(IS_GET){
			$id = $_GET["id"];
			$data = BottomModel::getInstance()->fetchOne("id=$id");
			$this->smarty->assign("data",$data);
			$this->smarty->display("Reception/bottom_edit.html");
		}else{
			$id = $_POST["id"];
			$content = $_POST["content"];
			$res = BottomModel::getInstance()->edit("content='$content'",$id);
			if($res){
				$this->write_log("修改前台底部部分");
				$this->jump("修改成功","?c=Reception&a=bottom_index");
			}else{
				$this->jump("修改失败","?c=Reception&a=bottom_index");
			}
		}
	}
}